package chapter07

/**
 *
 * @author cdhuangchao3
 * @date 2023/6/11 2:23 PM
 */
object Test18_ComplexWordCount {
  def main(args: Array[String]): Unit = {
    val tupleList = List(
      ("hello", 1),
      ("hello world", 2),
      ("hello scala", 3),
      ("hello spark from scala", 1),
      ("hello flink from scala", 2)
    )
    // 思路1：直接展开为普通版本
    val newStringList = tupleList.map(kv => (kv._1.trim + " ") * kv._2)
    val wordCountList = newStringList.flatMap(_.split(" "))
      .groupBy(word => word)
      .map(kv => (kv._1, kv._2.length))
      .toList
      .sortBy(_._2)(Ordering[Int].reverse)
      .take(3)
    println(wordCountList)
    println("=" * 20)

    // 思路2：直接基于预统计结果进行转换
    val preCountList = tupleList.flatMap(t => {
      val strings = t._1.split(" ")
      strings.map(d => (d, t._2))
    })
    println(preCountList)

    val grouMap = preCountList.groupBy(_._1)
    println(grouMap)
    //    val wordCountMap = grouMap.map(d => (d._1, d._2.reduce((e1:(String,Int), e2:(String,Int)) => e1._2 + e2._2)))
    //    val wordCountMap = grouMap.map(d => (d._1, d._2.map(_._2).sum))
    val wordCountMap = grouMap.mapValues(
      tupleList => tupleList.map(_._2).sum
    )
    println(wordCountMap)
    val wordCount = wordCountMap.toList
      .sortBy(_._2)(Ordering[Int].reverse)
      .take(3)
    println(wordCount)
  }
}
